﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Coding4Fun.BoxingBots.Common;

namespace Coding4Fun.BoxingBots.UserConsole
{
    /// <summary>
    /// Interaction logic for ringTimer.xaml
    /// </summary>
    public partial class ringTimer : UserControl
    {
    	private static readonly TimeSpan FullClock = TimeSpan.FromSeconds(Settings.RoundLength);
    	private TimeSpan _countdown = TimeSpan.Zero;

        public ringTimer()
        {
            InitializeComponent();
        }

        public void SetDuration(TimeSpan clock)
        {
            _countdown = FullClock - clock;
            if (_countdown < TimeSpan.Zero)
            {
                txtTens.Text = txtOnes.Text = txtTenth.Text = "0";
                return;
            }

            txtTens.Text = Math.Floor(_countdown.TotalSeconds / 10.0).ToString("0");
            txtOnes.Text = Math.Floor(_countdown.TotalSeconds % 10.0).ToString("0");
            txtTenth.Text = _countdown.Milliseconds.ToString("00").Substring(0,1);

            double sec = .98 - (.98 * (_countdown.TotalSeconds / Settings.RoundLength));
            drawTimeBlade(figure_ringRound, sec, 102.0, 23.0);

            double millisec = (_countdown.Milliseconds/1000.0);
            drawTimeBlade(figure_ringSecond, millisec, 76.0, 5.0);

        }

        private const Double maxTimeAngle = 360.0;

        private void drawTimeBlade(PathFigure blade, double angleNormal, double radius, double bladeWidth)
        {
            double angle = angleNormal * 6.28;
            Size innerRadius = new Size(radius, radius);
            Size outerRadius = new Size(radius + bladeWidth, radius + bladeWidth);
            Point innerArcStart = new Point(radius, 0.0);
            Point outerArcStart = new Point(radius + bladeWidth, 0.0);

            blade.Segments.Clear();
            blade.StartPoint = innerArcStart;
            blade.Segments.Add(new LineSegment(outerArcStart, true));

            bool isLarge = false;
            if (Math.Abs(angle) > 3.142) isLarge = true;

            blade.Segments.Add(new ArcSegment(
                new Point(Math.Cos(angle) * outerRadius.Height, Math.Sin(angle) * outerRadius.Height),
                outerRadius, 0, isLarge, SweepDirection.Clockwise, true));

            blade.Segments.Add(new LineSegment(
                new Point(Math.Cos(angle) * innerRadius.Width, Math.Sin(angle) * innerRadius.Width),
                true));

            blade.Segments.Add(new ArcSegment(innerArcStart, innerRadius, 0, isLarge, SweepDirection.Counterclockwise, true));

        }
    }
}
